package com.game.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;

/**
 * Created by tjc on 2019-3-16.
 * 加载图片类
 *
 * @auther: tjc
 * @Date: 2019-3-16
 */
public class ImageUtil {

    private static String IMG_ROOT = System.getProperty("user.dir");

    private static Logger log = LoggerFactory.getLogger(ImageUtil.class);

    public static final Toolkit tk = Toolkit.getDefaultToolkit();

    private static final String[] heroImagePaths =
            {
                    "images/tank/hero_up.gif",
                    "images/tank/hero_down.gif",
                    "images/tank/hero_left.gif",
                    "images/tank/hero_right.gif",
            };

    private static final String[] bulletImagePaths = {
            "images/bullet/bullet_up.png",
            "images/bullet/bullet_down.png",
            "images/bullet/bullet_left.png",
            "images/bullet/bullet_right.png",

    };

    private static final String[] wallImagePaths = {
            "images/wall/normal.gif",
    };

    private static final String[] tankBlastImagePaths = {
            "images/blast/tank_1.gif",
            "images/blast/tank_2.gif",
            "images/blast/tank_3.gif",
            "images/blast/tank_4.gif",
            "images/blast/tank_5.gif",
            "images/blast/tank_6.gif",
            "images/blast/tank_7.gif",
            "images/blast/tank_8.gif",
    };

    private static final String[] bulletBlastImagePaths = {
            "images/blast/bullet_1.png",
            "images/blast/bullet_2.png",
            "images/blast/bullet_3.png",
            "images/blast/bullet_4.png",
            "images/blast/bullet_5.png",
            "images/blast/bullet_6.png",
    };

    private static final String[] enemyImagePaths = {
            "images/tank/e_1_u.gif",
            "images/tank/e_1_d.gif",
            "images/tank/e_1_l.gif",
            "images/tank/e_1_r.gif"
    };
    private static final String[] numberImagePaths = {
            "images/number/0.png",
            "images/number/1.png",
            "images/number/2.png",
            "images/number/3.png",
            "images/number/4.png",
            "images/number/5.png",
            "images/number/6.png",
            "images/number/7.png",
            "images/number/8.png",
            "images/number/9.png",
    };
    private static final String appearImagePath = "images/other/appear.png";
    private static final String stageImagePath = "images/stage/stage.png";
    private static final String gameOverImagePath = "images/other/game_over.png";
    private static final String[] homeImagePaths = {"images/other/home.png", "images/other/home_destroyed.png"};


    public static final Image[] heroImages = ImageUtil.readImg(heroImagePaths);
    public static final Image[] enemyImages = ImageUtil.readImg(enemyImagePaths);
    public static final Image[] bulletImages = ImageUtil.readImg(bulletImagePaths);
    public static final Image[] wallImages = ImageUtil.readImg(wallImagePaths);
    public static final Image[] tankBlastImages = ImageUtil.readImg(tankBlastImagePaths);
    public static final Image[] bulletBlastImages = ImageUtil.readImg(bulletBlastImagePaths);
    public static final Image[] numberImages = ImageUtil.readImg(numberImagePaths);
    public static final Image appearImage = ImageUtil.readImg(appearImagePath);
    public static final Image stageImage = ImageUtil.readImg(stageImagePath);
    public static final Image gameOverImage = ImageUtil.readImg(gameOverImagePath);
    public static Image[] homeImages = ImageUtil.readImg(homeImagePaths);

    public static Image[] readImg(String... paths) {
        log.info("开始加载模型图片资源....");
        Image[] res = new Image[paths.length];
        for (int i = 0; i < paths.length; i++) {
            log.info("加载图片资源： " + ImageUtil.class.getClassLoader().getResource(paths[i]));
            res[i] = tk.getImage(ImageUtil.class.getClassLoader().getResource(paths[i]));
        }
        return res;
    }

    public static Image readImg(String path) {
        log.info("加载图片资源： " + ImageUtil.class.getClassLoader().getResource(path));
        Image res = tk.getImage(ImageUtil.class.getClassLoader().getResource(path));
        return res;
    }

    public static void loadAllImgs(Graphics g) {
        for (int i = 0; i < tankBlastImages.length; i++) {
            g.drawImage(tankBlastImages[i], Integer.MAX_VALUE, Integer.MAX_VALUE, null);
        }
        for (int i = 0; i < bulletImages.length; i++) {
            g.drawImage(bulletImages[i], Integer.MAX_VALUE, Integer.MAX_VALUE, null);
        }
        for (int i = 0; i < bulletBlastImages.length; i++) {
            g.drawImage(bulletBlastImages[i], Integer.MAX_VALUE, Integer.MAX_VALUE, null);
        }
        for (int i = 0; i < enemyImages.length; i++) {
            g.drawImage(enemyImages[i], Integer.MAX_VALUE, Integer.MAX_VALUE, null);
        }
        for (int i = 0; i < heroImages.length; i++) {
            g.drawImage(heroImages[i], Integer.MAX_VALUE, Integer.MAX_VALUE, null);
        }
        for (int i = 0; i < wallImages.length; i++) {
            g.drawImage(wallImages[i], Integer.MAX_VALUE, Integer.MAX_VALUE, null);
        }
        for (int i = 0; i < numberImages.length; i++) {
            g.drawImage(numberImages[i], Integer.MAX_VALUE, Integer.MAX_VALUE, null);
        }
        for (int i = 0; i < homeImages.length; i++) {
            g.drawImage(homeImages[i], Integer.MAX_VALUE, Integer.MAX_VALUE, null);
        }
        g.drawImage(appearImage, Integer.MAX_VALUE, Integer.MAX_VALUE, null);
        g.drawImage(stageImage, Integer.MAX_VALUE, Integer.MAX_VALUE, null);
        g.drawImage(gameOverImage, Integer.MAX_VALUE, Integer.MAX_VALUE, null);
    }
}
